package com.cris.service;

import com.cris.helper.DatabaseHelper;
import com.cris.model.ProductType;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by owen on 2017/3/16.
 */
@Service
public class ProductTypeService {
    /**
     * 获取产品类别列表
     */
    public List<ProductType> getProductTypeList(){
        String sql = "SELECT * FROM producttype";
        return DatabaseHelper.queryEntityList(ProductType.class,sql);
    }
    public List<ProductType> getProductTypeListById(long id){
        String sql = "SELECT * FROM producttype WHERE id=?";
        return DatabaseHelper.queryEntityList(ProductType.class,sql,id);
    }


    /**
     * 获取产品类别
     */
    public ProductType getProductType(long id){
        String sql = "SELECT * FROM producttype WHERE typeid=?";
        return DatabaseHelper.queryEntity(ProductType.class,sql,id);
    }


    /**
     * 获取子产品类别
     */
    public List<ProductType> getChildProductTypeList(){
        String sql = "SELECT * FROM producttype where parentid is not null";
        return DatabaseHelper.queryEntityList(ProductType.class,sql);
    }
    /**
     * 根据大类别id获得所有子类型
     */
    public List<ProductType> getChildProductTypeByParentTypeId(long id){
        String sql = "select * from producttype where producttype.parentid='"+id+"'";
        return DatabaseHelper.queryEntityList(ProductType.class,sql);
    }
    /**
     * 根据产品id获取该大类下的所有子类型
     */
    public List<ProductType> getChildProductTypeListById(long id){
        String sql = "SELECT * FROM producttype where parentid=(select producttype.parentid from product,producttype where product.id='"+id+"' and product.typeid=producttype.typeid)";
        return DatabaseHelper.queryEntityList(ProductType.class,sql);
    }

    /**
     * 获取大类别
     */
    public List<ProductType> getParentProductTypeList(){
        String sql = "SELECT * FROM producttype where parentid is null";
        return DatabaseHelper.queryEntityList(ProductType.class,sql);
    }

    /**
     * 删除产品类别
     */
    public boolean deleteProductType(long id){
        return DatabaseHelper.deleteEntity(ProductType.class,id);
    }

    /**
     * 更新产品类别
     */
    public boolean updateProductType(long id,Map<String,Object> fieldMap){

        return DatabaseHelper.updateEntity(ProductType.class,id,fieldMap);
    }

    /**
     * 添加商品类别
     */
    public boolean addProductType(Map<String,Object> fieldMap){
        return DatabaseHelper.insertEntity(ProductType.class,fieldMap);
    }


}
